<?php
/**
 * Created by PhpStorm.
 * User: zcq
 * Date: 2018/10/15
 * Time: 15:41
 */

namespace app\index\controller;


use app\common\Sms;
use app\index\service\VoteService;

class Vote extends Base
{
    public $voteService;

    public function __construct(VoteService $voteService)
    {
        parent::__construct();
        $this->assign('site_id', $this->site_id);
        $this->voteService = $voteService;
    }

    /**
     * @return mixed
     * 主页，分类的话显示分类页面，没分类显示投票信息列表
     */
    public function index()
    {
        $vote_id = input('vote_id');
        $vote = $this->getVote($vote_id);

        $this->assign('title', $vote['name']);
        if ($vote['type_enable']) {
            $type = $this->voteService->getVoteType($vote_id);
            $this->assign('type', json_encode($type, JSON_UNESCAPED_UNICODE));
            return $this->fetch('class');
        } else {
            $this->assign('type_id', 0);
            $this->assign('banner', $vote['head_img']);

            return $this->fetch('index');
        }
    }

    /**
     * 两会列表
     */
    public function meetings()
    {
        $vote_id = input('id');
        $vote = $this->getVote($vote_id);

        $this->assign('title', $vote['name']);
        $this->assign('banner', $vote['head_img']);

        return $this->fetch('meetings');

    }

    /**
     * 两会代表数据
     *
     */
    public function getMeetingList()
    {
        $voteId = input('vote_id/d');
        $order = $this->params['order'] ?? '';
        $keyword = $this->params['keyword'] ?? '';
        $typeId = input('type_id/d');
        //查询缓存
        $data = $this->voteService->getItems($voteId, $typeId, $order, $keyword,false);

//        dump($data);
//        foreach ($data as &$v){
//            $v['remark'] = $v['remark']?explode('|',$v['remark']):[];
//        }

        return jsonReturn(0,'success',['list'=>$data]);
    }

    /**
     * @return mixed
     * 分类投票信息列表
     */
    public function classify()
    {
        $type_id = input('type_id/d');
        $type = $this->voteService->getTypeById($type_id);
        if (!$type)
            $this->error('分类不存在');
        $vote = $this->getVote($type['vote_id']);
        $this->assign('type', json_encode($type, JSON_UNESCAPED_UNICODE));
        $this->assign('type_id', $type_id);
        $this->assign('banner', $vote['head_img']);
        $this->assign('title', $type['name']);

        return $this->fetch('index');
    }

    /**
     * @return mixed
     * 选手详情页
     */
    public function detail()
    {
        $itemId = input('item_id/d');
        $item = $this->voteService->getItemById($itemId);
        if (!$item) {
            $this->error('不存在');
        }

        $vote = $this->getVote($item['vote_id']);
        $this->assign('title', $item['name']);

        $this->assign('item', json_encode($item, JSON_UNESCAPED_UNICODE));
        if ($vote['type_enable']) {
            $this->assign('banner', $item['type_head_img']);
        } else {
            $this->assign('banner', $vote['head_img']);

        }
        return $this->fetch();
    }

    /**
     * @param $params
     * 批量查询
     */
    public function getList()
    {
        $voteId = input('vote_id/d');
        $order = $this->params['order'] ?? '';
        $keyword = $this->params['keyword'] ?? '';
        $typeId = input('type_id/d');
        //查询缓存
        $data = $this->voteService->getItems($voteId, $typeId, $order, $keyword);

        return jsonReturn(0, 'success', ['list' => $data]);
    }

    /**
     * @return \think\response\Json
     * 投票
     */
    public function vote()
    {
        $voteId = input('vote_id/d');
        $itemId = input('item_id/d');
        if ($loginError = $this->isLogin()) {
            return $loginError;
        }

        $res = $this->voteService->vote($voteId, $itemId, $this->mobile, $this->ip,$this->device_id);
        return jsonReturn($res['code'], $res['msg'], $res['data']);
    }

    /**
     * 获取评论列表
     */
    public function getCommentList()
    {
        $voteItemId = input('vote_item_id/d');
        if (!$voteItemId)
            return jsonReturn(1, '参数错误');
//        $vote = $this->getVote($voteId);
        $list = $this->voteService->getComment($voteItemId);
        return jsonReturn(0, 'success', $list);
    }

    /**
     * @return bool|false|string|\think\response\Json
     * 添加评论
     */
    public function addComment()
    {
        $voteItemId = input('vote_item_id/d');
        $content = input('content');
        if (!$voteItemId || $content == '')
            return jsonReturn(1, '参数错误');
        if ($loginError = $this->isLogin()) {
            return $loginError;
        }
        $res = $this->voteService->addComment($voteItemId, $content, $this->mobile, $this->ip);
        return jsonReturn($res['code'], $res['msg'], $res['data']);
    }

    /**
     * @param $voteId
     * @return array|bool|false|int|mixed|\PDOStatement|string|\think\Model
     * 查询投票是否存在
     */
    public function getVote($voteId)
    {
        $vote = $this->voteService->getVote($voteId);
        if (!$vote) {
            $this->error('活动不存在');
        }
        $vote['start_time'] = $this->voteService->formatTime($vote['start']);
        $vote['end_time'] = $this->voteService->formatTime($vote['end']);
        $vote['vote_btn_name'] = $vote['vote_btn_name']??'投票';
        $this->assign('vote', $vote);
        return $vote;
    }

    /**
     * 发送验证码
     */
    public function send_code()
    {
        $phone = input('phone');
        if($phone == '' || !preg_match("/^\d{11}$/",$phone)){
            return dataReturn(0,'号码格式错误');
        }
        $res = Sms::instance()->send_code($phone);
        return $res;
    }

    public function web_login()
    {
        //已登录返回false
        if (!$this->isLogin()) {
            return dataReturn(0,'用户已登录');
        }
        $phone = input('phone');
        if($phone == '' || !preg_match("/^\d{11}$/",$phone)){
            return dataReturn(0,'号码格式错误');
        }
        $code = input('code');
        if($code == '')
            return dataReturn(0,'验证码不能未空');
        if(!Sms::instance()->validate($phone,$code))
            return dataReturn(0,'验证码错误');

        session('mobile',$phone);
        return dataReturn(1,'登录成功');
    }

    public function web_logout()
    {
        session('mobile',null);
        session('token',null);
        session('device_id',null);
        return dataReturn(1,'退出成功');
    }
}